package com.hdoit.ares.logical.acc.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.acc.dao.IGoldenBeanDetailDao;
import com.hdoit.ares.logical.acc.model.AccountInfo;
import com.hdoit.ares.logical.acc.model.GoldenBeanDetail;
import com.hdoit.ares.logical.acc.service.IAccountInfoService;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.acti.model.ActiTask;
import com.hdoit.ares.logical.acti.model.TaskGoldenBean;
import com.hdoit.ares.logical.acti.service.IActiTaskService;
import com.hdoit.ares.logical.acti.service.ITaskGoldenBeanService;
import com.hdoit.ares.logical.viewmodel.acc.FindGoldenDetail;
import com.hdoit.ares.logical.viewmodel.acc.GoldenBeanDetailSearch;
import com.hdoit.ares.selfcommon.enums.acc.HesitatePeriodState;
import com.hdoit.ares.selfcommon.enums.acc.State;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.RandomUtils;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月29日 20:01:01
 * @comment
 */
@Component
public class GoldenBeanDetailServiceImpl extends SuperService<GoldenBeanDetail> implements IGoldenBeanDetailService{
	
	private IAccountInfoService accountInfoService;
	private IActiTaskService actiTaskService;
	private ITaskGoldenBeanService taskGoldenBeanService;
	
	
	public ITaskGoldenBeanService getTaskGoldenBeanService() {
		return taskGoldenBeanService;
	}
	@Resource
	public void setTaskGoldenBeanService(ITaskGoldenBeanService taskGoldenBeanService) {
		this.taskGoldenBeanService = taskGoldenBeanService;
	}
	public IAccountInfoService getAccountInfoService() {
		return accountInfoService;
	}
	@Resource
	public void setAccountInfoService(IAccountInfoService accountInfoService) {
		this.accountInfoService = accountInfoService;
	}

	public IActiTaskService getActiTaskService() {
		return actiTaskService;
	}
	@Resource
	public void setActiTaskService(IActiTaskService actiTaskService) {
		this.actiTaskService = actiTaskService;
	}

	private IGoldenBeanDetailDao goldenBeanDetailDao;
	public IGoldenBeanDetailDao getGoldenBeanDetailDao() {
		return goldenBeanDetailDao;
	}

	@Resource
	public void setGoldenBeanDetailDao(IGoldenBeanDetailDao goldenBeanDetailDao) {
		this.goldenBeanDetailDao = goldenBeanDetailDao;
	}

	@Override
	public List<GoldenBeanDetail> queryList(GoldenBeanDetailSearch search, PagerModel pagerModel) {
		return goldenBeanDetailDao.queryList(search,pagerModel);
	}

	@Override
	public List<GoldenBeanDetail> queryList(GoldenBeanDetailSearch search) {
		return goldenBeanDetailDao.queryList(search);
	}

	@Override
	public int queryCount(GoldenBeanDetailSearch search) {
		return goldenBeanDetailDao.queryCount(search);
	}

	@Override
	public List<GoldenBeanDetail> queryBonusList(GoldenBeanDetailSearch search, String id) {
		
		return goldenBeanDetailDao.queryBonusList(search, id);
	}

	@Override
	public GoldenBeanDetail querySumIncomeMoney(FindGoldenDetail findGoldenDetail,String id) {
		
		return goldenBeanDetailDao.querySumIncomeMoney(findGoldenDetail,id);
	}

	@Override
	public GoldenBeanDetail querySumExpendMoney(FindGoldenDetail findGoldenDetail,String id) {
		
		return goldenBeanDetailDao.querySumExpendMoney(findGoldenDetail,id);
	}

	@Override
	public List<GoldenBeanDetail> queryGoldenBeanDetail(FindGoldenDetail findGoldenDetail, String custId) {
		
		return goldenBeanDetailDao.queryGoldenBeanDetail(findGoldenDetail, custId);
	}

	public Boolean updateHesitatePeriodState(Date lastDate) {
		return goldenBeanDetailDao.updateHesitatePeriodState(lastDate);
	}
    
	@Override
	public GoldenBeanDetail queryGoldenInHesitate(String id) {
		return goldenBeanDetailDao.queryGoldenInHesitate(id);
	}


	@Override
	public GoldenBeanDetail queryGoldenBean(String id) {
		
		return goldenBeanDetailDao.queryGoldenBean(id);
	}

	//分页查询金豆详情列表 
	@Override
	public List<GoldenBeanDetail> queryGoldenList(FindGoldenDetail findGoldenDetail, String custId, Integer start,Integer length) {
		
		return goldenBeanDetailDao.queryGoldenList(findGoldenDetail, custId, start, length);
	}

	@Override
	public GoldenBeanDetail queryModelBySearch(GoldenBeanDetailSearch search) {
		return goldenBeanDetailDao.queryModelBySearch(search);
	}


	@Override
	public boolean addGoldenBeanDetail(String taskId, int goldenBeanInt, String goldenBeanType, String customerId) {
		ActiTask actiTask = actiTaskService.queryModelByTaskId(taskId);
		int goldenBean = Integer.valueOf(actiTask.getReward());
		if (StringUtils.isEmpty(actiTask.getReward())) {
			goldenBean = goldenBeanInt;
		}
		AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customerId);
		accountInfo.setGoldenBean(accountInfo.getGoldenBean() + goldenBean);
		accountInfo.setModelStatus(ModelStatus.update);
		if(!accountInfoService.applyChange(accountInfo)){
			/*throw new RuntimeException("error add acountInfo by task"+"***"+taskId+"***"+customerId);*/
			return false;
		}
		TaskGoldenBean taskGoldenBean = taskGoldenBeanService.queryModelByCustomerId(customerId);
		taskGoldenBean.setGoldenBean(taskGoldenBean.getGoldenBean() + goldenBean);
		taskGoldenBean.setModelStatus(ModelStatus.update);
		if(!taskGoldenBeanService.applyChange(taskGoldenBean)){
			/*throw new RuntimeException("error add acountInfo by task"+"***"+taskId+"***"+customerId);*/
			return false;
		}

		// 金豆明细
		GoldenBeanDetail goldenBeanDetail = new GoldenBeanDetail();
		goldenBeanDetail.setId(CommonUtils.getKeyId());
		goldenBeanDetail.setAccountNo(accountInfo.getAccountNo());
		goldenBeanDetail.setGoldenBeanType(goldenBeanType);
		goldenBeanDetail.setTime(DateUtils.getCurrent());
		goldenBeanDetail.setState(State.income);
		goldenBeanDetail.setGoldenBeanAccount(goldenBean);
		goldenBeanDetail.setGoldenBeanBalance(accountInfo.getGoldenBean());
		goldenBeanDetail.setCustomerId(customerId);
		goldenBeanDetail.setOutTradeNo("" + System.currentTimeMillis() + RandomUtils.randomInt(1000, 9999));
		goldenBeanDetail.setComment("task");
		goldenBeanDetail.setHesitatePeriodState(HesitatePeriodState.Hesitated);
		goldenBeanDetail.setModelStatus(ModelStatus.add);
		if(!applyChange(goldenBeanDetail)){
			/*throw new RuntimeException("error add goldenBeanDetail by task"+"***"+taskId+"***"+customerId);*/
			return false;
		}
		return true;
	}
	


	/*@Override
	public GoldenBeanDetail querySumIncome(FindGoldenDetail findGoldenDetail,String id, Integer start, Integer length) {
		
		return goldenBeanDetailDao.querySumIncome(findGoldenDetail, id, start, length);
	}

	@Override
	public GoldenBeanDetail querySumExpend(FindGoldenDetail findGoldenDetail,String id, Integer start, Integer length) {
		
		return goldenBeanDetailDao.querySumExpend(findGoldenDetail, id, start, length);
	}*/

}
